﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class Customer_Contract_Add : System.Web.UI.Page
{
    protected int ContractKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ContractKey = string.IsNullOrEmpty(Request["ContractKey"]) ? 0 : Convert.ToInt32(Request["ContractKey"]);

        if (!this.IsPostBack)
        {


            txt_StartDate.Attributes.Add("onclick", "selectDate(this);");
            txt_EndDate.Attributes.Add("onclick", "selectDate(this);");

            if (ContractKey != 0)
            {
                Dat_Contract contract = new Contract().SelectContract(ContractKey);

                txt_MUserKey.Value = contract.MUserKey.ToString();
                txt_CustName.Text = new Account().GetAccountCompanyName(contract.MUserKey);
                txt_ContractType.SelectedValue = contract.ContractType;
                txt_ContractNO.Text = contract.ContractNO;
                txt_StartDate.Text = WebUtility.DateTimeToShortString(contract.StartDate);
                txt_EndDate.Text = WebUtility.DateTimeToShortString(contract.EndDate);
                txt_ContractPrice.Text = WebUtility.DecimalToString(contract.ContractPrice);
                txt_PayPeriod.Text = contract.PayPeriod;
                txt_Remark.Text = contract.Remark;

                btnSubmit.Text = "修 改";
            }
       }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime? de = null;

         if (ContractKey == 0)
        {
            Dat_Contract contract = new Dat_Contract
            {
                MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                ContractType = txt_ContractType.SelectedValue,
                ContractNO = txt_ContractNO.Text.Trim(),
                StartDate =  Convert.ToDateTime(txt_StartDate.Text.Trim()),
                EndDate = Convert.ToDateTime(txt_EndDate.Text.Trim()),
                ContractPrice = Convert.ToDecimal(txt_ContractPrice.Text.Trim()),
                PayPeriod = txt_PayPeriod.Text.Trim(),
                Remark = txt_Remark.Text.Trim(),
                CreateTime = DateTime.Now,
                UserKey = Convert.ToInt32(Session["UserKey"])
            };

            if (new Contract().AddContract(contract))
            {
                WebUtility.JsAlertToGo("添加合同成功", "Contract.aspx?menutype=422");
            }
        }
        else
        {
            Dat_Contract contract = new Dat_Contract
            {
                ContractKey = ContractKey,
                MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                ContractType = txt_ContractType.SelectedValue,
                ContractNO = txt_ContractNO.Text.Trim(),
                StartDate = Convert.ToDateTime(txt_StartDate.Text.Trim()),
                EndDate = Convert.ToDateTime(txt_EndDate.Text.Trim()),
                ContractPrice = Convert.ToDecimal(txt_ContractPrice.Text.Trim()),
                PayPeriod = txt_PayPeriod.Text.Trim(),
                Remark = txt_Remark.Text.Trim(),
            };

            if (new Contract().EditContract(contract))
            {
                WebUtility.JsAlertToGo("修改合同成功", "Contract.aspx?menutype=422");
            }
        }
    }

}
